<?php

namespace App\Http\Controllers\supplier;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\supplier\SmsController;

class SupplierController extends Controller
{
    //登陆发送短信
    public function supplier_sendSms_login(Request $request){
        $params = $request->all();
        if (empty($params['phone'])) {
            return $this->sendError('参数传递错误');
        }
        $phone          = $params['phone'];
        $respond_code   = SmsController::supplier_sendSms_login($phone);
        if ($respond_code == false){
            return $this->sendError('短信发送失败');
        }
        $redis          = new \redis();
        $redis->connect('127.0.0.1',6379);

        $key            = 'supplier_logon'.$phone;//用手机号作为key值
        $code           = json_decode($respond_code,true);//接收刚刚发出的短信code

        $redis->set($key,$code,300);//设置的5分钟有效期

        return  $this->sendResponse([],'短信发送成功');
    }
    //供应商注册
    public function supplier_register(Request $request){
        $params = $request->all();
        if (empty($params['phone'])){
            return $this->sendError('手机号不能为空');
        }
        if (empty($params['login_password'])){
            return $this->sendError('请设置登录密码');
        }
        if (empty($params['actual_name'])){
            return $this->sendError('请填写真实姓名');
        }
        if (empty($params['id_number'])){
            return $this->sendError('请填写身份证号码');
        }
        if ($this->check_id_card($params['id_number']) == false){
            return $this->sendError('请填写正确的身份证号');
        }
        if (empty($params['company_name'])){
            return $this->sendError('请填写公司名称');
        }
        if (empty($params['business_license_number'])){
            return $this->sendError('请填写营业执照号码');
        }
        $info = [
            'login_password'=>md5($params['login_password']),
            'actual_name'=>$params['actual_name'],
            'id_number'=>$params['id_number'],
            'company_name'=>$params['company_name'],
            'business_license_number'=>$params['business_license_number'],
            'phone'=>$params['phone'],
            'business_number'=>'1'.rand(0000000,99999999)
        ];
        $res = Db::connection('mysql_HT')->table('supplier')->insert($info);

        if (!$res){
            return $this->sendError('手机号已被注册');
        }
        return $this->sendResponse([],'注册成功');
    }
    //判断手机号是否已经注册
    public function supplier_is_phone(Request $request){
        $params = $request->all();
        if (empty($params['phone'])){
            return $this->sendError('请输入手机号');
        }
        if ($this->is_phone($params['phone']) == false){
            return $this->sendError('请输入正确的手机号码');
        }

        $res = Db::connection('mysql_HT')->table('supplier')->where('phone',$params['phone'])->first();
        if (!empty($res)){
            return $this->sendError('手机号已被注册');
        }
        return $this->sendResponse([],'验证通过');
    }
    //供应商忘记密码
    public function supplier_no_password(Request $request){
        $params = $request->all();
        if (empty($params['phone'])){
            return $this->sendError('请输入手机号');
        }
        if (empty($params['code'])){
            return $this->sendError('请输入验证码');
        }
        if ($this->is_phone($params['phone']) == false){
            return $this->sendError('请输入正确的手机号码');
        }
        $phone          = $params['phone'];
        $code           = $params['code'];

        $redis          = new \redis();
        $redis->connect('127.0.0.1',6379);

        //验证验证码
        $redis          = new \redis();
        $redis->connect('127.0.0.1',6379);//连接redis

        $value  = $redis->get('supplier_logon'.$phone);

        if (empty($value)) {
            return $this->sendError('验证码已失效');
        }
        if ($value != $code) {
            return $this->sendError('验证码错误');
        }
        $redis->del($value);
        $res = Db::connection('mysql_HT')->table('supplier')->where('phone',$params['phone'])->update(['password'=>md5($params['password'])]);
        if (!$res){
            return $this->sendError('重置失败');
        }
        return $this->sendResponse([],'重置密码成功');
    }
    //供应商登录
    public function supplier_logon(){
        
    }
}
